package client.gui;

import it.randomtower.engine.ME;
import network.Globals;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;

import utils.ResourceManager;
import client.gui.base.GUIElement;
import client.states.WorldState;
import entities.base.AbstractPlayer;
import entities.playable.PlayerRoper;

public class HUD extends GUIElement {

	private static final float BORDER_PADDING = 15;

	private Image roperDisplay = null;
	private Image aboboDisplay = null;

	private WorldState world = null;

	private boolean roperReady = false;
	private boolean aboboReady = false;

	private int roperHP = 0;
	private int roperWeaponHeat = 0;

	private int aboboHP = 0;

	public HUD(WorldState worldState) {
		world = worldState;

		roperDisplay = ResourceManager.getImage("charRoper").getScaledCopy(.3f);
		aboboDisplay = ResourceManager.getImage("charAbobo").getScaledCopy(.3f);

		update(0);

		addFont("normal", "data/fonts/capture.ttf", 16, false, false, java.awt.Color.white);
	}

	@Override
	public void update(int delta) {
		AbstractPlayer abobo = (AbstractPlayer) world.find(Globals.ENTITIY_ABOBO);
		if (abobo != null) {
			aboboHP = abobo.getCurHealth();
			aboboReady = true;
		}

		PlayerRoper roper = (PlayerRoper) world.find(Globals.ENTITIY_ROPER);
		if (roper != null) {
			roperHP = roper.getCurHealth();
			roperWeaponHeat = roper.getWeaponHeat();
			roperReady = true;
		}
	}

	@Override
	public void render(Graphics g) {
		Color bgColor = Color.lightGray;
		bgColor.a = .3f;
		g.setColor(bgColor);
		g.fillRect(0, 0, Globals.APP_WIDTH, BORDER_PADDING + roperDisplay.getWidth());

		roperDisplay.draw(BORDER_PADDING, BORDER_PADDING);
		aboboDisplay.draw(Globals.APP_WIDTH - aboboDisplay.getWidth() - BORDER_PADDING, BORDER_PADDING);

		renderLifeBar(g);
		renderWeaponHeat(g);

		// Roper
		getFont("normal").drawString(BORDER_PADDING + roperDisplay.getWidth() + 10, BORDER_PADDING, roperHP + "%");

		// Abobo
		getFont("normal").drawString(Globals.APP_WIDTH - BORDER_PADDING - aboboDisplay.getWidth() - getFont("normal").getWidth(aboboHP + "%") - 10, BORDER_PADDING, aboboHP + "%");

		// Player names
		if (ME.attributes.get("playerOne").equals(Globals.ENTITIY_ABOBO))
			getFont("normal").drawString(world.camera.x + world.find(Globals.ENTITIY_ABOBO).x + world.find(Globals.ENTITIY_ABOBO).hitboxWidth / 2 - getFont("normal").getWidth(ME.attributes.get("playerOneName").toString()) / 2, world.camera.y + world.find(Globals.ENTITIY_ABOBO).y - 40, ME.attributes.get("playerOneName").toString());
		else
			getFont("normal").drawString(world.camera.x + world.find(Globals.ENTITIY_ROPER).x + world.find(Globals.ENTITIY_ROPER).hitboxWidth / 2 - getFont("normal").getWidth(ME.attributes.get("playerOneName").toString()) / 2, world.camera.y + world.find(Globals.ENTITIY_ROPER).y - 40, ME.attributes.get("playerOneName").toString());

		if (ME.attributes.get("playerTwo").equals(Globals.ENTITIY_ABOBO))
			getFont("normal").drawString(world.camera.x + world.find(Globals.ENTITIY_ABOBO).x + world.find(Globals.ENTITIY_ABOBO).hitboxWidth / 2 - getFont("normal").getWidth(ME.attributes.get("playerTwoName").toString()) / 2, world.camera.y + world.find(Globals.ENTITIY_ABOBO).y - 40, ME.attributes.get("playerTwoName").toString());
		else
			getFont("normal").drawString(world.camera.x + world.find(Globals.ENTITIY_ROPER).x + world.find(Globals.ENTITIY_ROPER).hitboxWidth / 2 - getFont("normal").getWidth(ME.attributes.get("playerTwoName").toString()) / 2, world.camera.y + world.find(Globals.ENTITIY_ROPER).y - 40, ME.attributes.get("playerTwoName").toString());
	}

	private void renderWeaponHeat(Graphics g) {
		Color old = g.getColor();

		int red = (255 * roperWeaponHeat) / 100;
		int green = (255 * (100 - roperWeaponHeat)) / 100;
		int blue = 0;

		Color fillColor = new Color(red, green, blue);

		float x = BORDER_PADDING + roperDisplay.getWidth() + 10 - 1;
		float y = BORDER_PADDING * 4.2f - 1;

		g.setColor(Color.black);
		g.drawRect(x, y, 100 + 1, 5);
		g.setColor(fillColor);
		g.fillRect(x + 1, y + 1, 100 * roperWeaponHeat / 100, 5 - 1);

		g.setColor(old);
	}

	/**
	 * Zeichnet die Lebensbalken der Spieler.
	 * 
	 * @param g
	 *            - Graphikobjekt
	 */
	private void renderLifeBar(Graphics g) {
		Color old = g.getColor();

		// Roper
		int red = (255 * (100 - roperHP)) / 100;
		int green = (255 * roperHP) / 100;
		int blue = 0;

		Color fillColor = new Color(red, green, blue);

		g.setColor(Color.black);
		g.drawRect(BORDER_PADDING + roperDisplay.getWidth() + 10 - 1, BORDER_PADDING * 2.8f - 1, 100 + 1, getFont("normal").getHeight(roperHP + "%") + 1);
		g.setColor(fillColor);
		g.fillRect(BORDER_PADDING + roperDisplay.getWidth() + 10, BORDER_PADDING * 2.8f, fillColor.g * 100, getFont("normal").getHeight(roperHP + "%"));

		// Abobo
		red = (255 * (100 - aboboHP)) / 100;
		green = (255 * aboboHP) / 100;
		blue = 0;

		fillColor = new Color(red, green, blue);

		g.setColor(Color.black);
		g.drawRect(Globals.APP_WIDTH - BORDER_PADDING - aboboDisplay.getWidth() - 10 - (100 + 1), BORDER_PADDING * 2.8f - 1, 100 + 1, getFont("normal").getHeight(roperHP + "%") + 1);
		g.setColor(fillColor);
		g.fillRect(Globals.APP_WIDTH - BORDER_PADDING - aboboDisplay.getWidth() - 10, BORDER_PADDING * 2.8f, -fillColor.g * 100, getFont("normal").getHeight(roperHP + "%"));

		g.setColor(old);
	}

}
